package com.lebang.controller.sys;

import com.lebang.common.Login;
import com.lebang.component.RedisClient;
import com.lebang.domain.TaskRouseDetail;
import com.lebang.model.TaskInitModel;
import com.lebang.model.Whether;
import com.lebang.model.enums.OsVersionEnum;
import com.lebang.model.enums.buss.DownStatus;
import com.lebang.service.CpaService;
import com.lebang.service.DownAppService;
import com.lebang.service.TaskRouseDetailService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;

import static com.lebang.service.CpaService.ZY_ROUSE_DETAIL;


/**
 * 数据源下载任务详情
 *
 * @author hwq
 * @date 2020-01-05
 */
@Controller
@RequestMapping("/sys/taskrousedetail")
public class TaskRouseDetailController {
    @Autowired
    private TaskRouseDetailService baseService;
    @Autowired
    private CpaService cpaService;
    @Autowired
    private DownAppService downAppService;
    @Autowired
    private RedisClient redisClient;
    @Login
    @GetMapping("/list")
    public String list(Model model, long rouseId) {

        model.addAttribute("rouseId", rouseId);
        return "/taskrousedetail/list";
    }
    @Login
    @GetMapping("/cpalist")
    public String cpalist(Model model, long rouseDetailId) {
        model.addAttribute("rouseDetailId", rouseDetailId);
        return "/taskrousedetail/cpa_list";
    }
    @Login
    @GetMapping("/add")
    public String add(Long id, Model model, Long rouseId) {
        TaskRouseDetail vo;
        if (id != null) {
            vo = baseService.selectById(id);
        } else {
            vo = new TaskRouseDetail();
            vo.setRouseId(rouseId);
            vo.setIsClick(Whether.YES.getDbIndex());
            vo.setIsFirst(Whether.YES.getDbIndex());
            vo.setIsMy(Whether.YES.getDbIndex());
            vo.setHasDeleted(Whether.NO.getDbIndex());
            vo.setRewardType(Whether.NO.getDbIndex());
            vo.setSosoType(Whether.YES.getDbIndex());
            vo.setOpenFiter(Whether.NO.getDbIndex());
            vo.setBrand("all");
            vo.setOvsType(OsVersionEnum.ANDROID_10.getIndex());

        }

        model.addAttribute("rouseId", rouseId);
        model.addAttribute("item", vo);
        return "/taskrousedetail/edit";
    }



    @Login
    @PostMapping("/edit")
    @ResponseBody
    public JsonData edit(TaskRouseDetail vo) {
        vo.setIsMy(Whether.YES.getDbIndex());
        if (vo.getIsClick().equals(Whether.YES.getDbIndex())) {
            if (TextUtil.isEmpty(vo.getStrategyId())) {
                return Callback.fail("success", "设置签到后请设置签到规则");
            }
        }
        if (vo.getId() != null) {
            vo.setModifyTime(LocalDateTime.now());
            vo.setSuccessCount(null);
            vo.setFailCount(null);
            baseService.updateById(vo);
        } else {
            vo.setEnable(Whether.NO.getDbIndex());
            vo.setCreateTime(LocalDateTime.now());
            vo.setHasDeleted(Whether.NO.getDbIndex());
            vo.setSuccessCount(0);
            vo.setFailCount(0);
            vo.setTodaySuccessCount(0);
            baseService.insertModel(vo);
        }
        return Callback.success("success", "修改成功");
    }

    @Login
    @PostMapping("/enable")
    @ResponseBody
    public JsonData enable(Long id, Integer enable) {
        TaskRouseDetail rouse = baseService.selectById(id);
        rouse.setEnable(enable);
        rouse.setModifyTime(LocalDateTime.now());
        baseService.updateById(rouse);
        if(enable==0){
            redisClient.hdel(ZY_ROUSE_DETAIL, String.valueOf(rouse.getId()));
        }else {
            this.pool(id);
        }
        return Callback.success("success", "修改成功");
    }

    @Login
    @PostMapping("/click")
    @ResponseBody
    public JsonData click(Long id, Integer enable) {
        TaskRouseDetail rouse = baseService.selectById(id);
        rouse.setIsClick(enable);
        rouse.setModifyTime(LocalDateTime.now());
        baseService.updateById(rouse);

        return Callback.success("success", "修改成功");
    }

    @Login
    @PostMapping("/first")
    @ResponseBody
    public JsonData first(Long id, Integer enable) {
        TaskRouseDetail rouse = baseService.selectById(id);
        rouse.setIsFirst(enable);
        rouse.setModifyTime(LocalDateTime.now());
        baseService.updateById(rouse);

        return Callback.success("success", "修改成功");
    }
    @Login
    @PostMapping("/pool")
    @ResponseBody
    public JsonData pool(Long id) {
        TaskRouseDetail rouse = baseService.selectById(id);
        int s = downAppService.getByStatus(id, DownStatus.SUCCESS.getIndex(),cpaService.getZyData() );
        int f = downAppService.getByStatus(id, DownStatus.RELSE.getIndex(),cpaService.getZyData() );
        rouse.setSuccessCount(s);
        rouse.setFailCount(f);
        baseService.updateById(rouse);
        TaskInitModel model=new TaskInitModel();
        model.setId(rouse.getId());
        model.setAmount(rouse.getDayAmount());
        model.setSuccessTotal(rouse.getSuccessCount());
        model.setTodayTotal(rouse.getTodaySuccessCount());
        model.setTotal(rouse.getRecordCount());
         cpaService.taskInitByTaskModel(model);
        return Callback.success("success", "加载成功.");
    }


    /**
     * 复制自营任务
     * @param id
     * @param model
     * @param rouseId
     * @return
     */
    @Login
    @GetMapping("/copy")
    public String copy(Long id, Model model, Long rouseId) {
        TaskRouseDetail vo = baseService.selectById(id);
        LocalDateTime now = LocalDateTime.now();
        vo.setId(null);
        vo.setCreateTime(now);
        vo.setModifyTime(now);
        vo.setEnable(Whether.NO.getDbIndex());
        vo.setHasDeleted(Whether.NO.getDbIndex());
        vo.setSuccessCount(0);
        vo.setFailCount(0);
        vo.setTodaySuccessCount(0);
        model.addAttribute("rouseId", rouseId);
        model.addAttribute("item", vo);
        return "/taskrousedetail/edit";
    }


}
